﻿namespace EFS.RC.CC.CH.Tables
{
	public static class SurveyCategories
	{
		/// Returns an object from a data row
		public static EFS.RC.CC.CH.Entities.Surveys.Category FromTableRow(System.Data.DataRow row)
		{
			// check
			EFS.Common.Verify.Reference(row, "row");

			// create new
			EFS.RC.CC.CH.Entities.Surveys.Category dataObj = new EFS.RC.CC.CH.Entities.Surveys.Category();

			// ID
			dataObj.ID = EFS.DB.DataRow.Int32(row, "ID");

			// ID
			dataObj.SurveyID = EFS.DB.DataRow.Int32(row, "SURVEY_ID");

			// NAME
			dataObj.Name = EFS.DB.DataRow.String(row, "NAME");

			// PRIORITY
			dataObj.Priority = EFS.DB.DataRow.Int32(row, "PRIORITY");

			// ok
			return dataObj;
		}

		/// GetAll
		public static EFS.RC.CC.CH.Entities.Surveys.Category[] GetBySurveyID(System.Int32 surveyId)
		{
			// handle connection
			using (EFS.DB.Connection connection = EFS.RC.CC.CH.QRDConnection.New)
			{
				// get data
				return connection.DoExecuteTableReaderQuery<EFS.RC.CC.CH.Entities.Surveys.Category>(
					"[dbo].[SurveyCategories_GetBySurveyID]",
					new EFS.DB.Parameters.Array("survey_id", surveyId),
					FromTableRow);
			}
		}

		/// GetByID
		public static EFS.RC.CC.CH.Entities.Surveys.Category GetByID(System.Int32 id)
		{
			// handle connection
			using (EFS.DB.Connection connection = EFS.RC.CC.CH.QRDConnection.New)
			{
				// get data
				return connection.DoExecuteRowReaderQuery<EFS.RC.CC.CH.Entities.Surveys.Category>(
					"[dbo].[SurveyCategories_GetByID]",
					new EFS.DB.Parameters.Array("id", id),
					FromTableRow);
			}
		}

		/// Create
		public static System.Int32? Create(
			System.Int32 surveyId,
			System.String name,
			System.Int32 priority)
		{
			// make params
			EFS.DB.Parameters.Array parametersArray = new EFS.DB.Parameters.Array(
				new System.String[] { "name", "survey_id", "priority", "id" },
				new System.Object[] { name, surveyId, priority, default(System.Int32) },
				new System.Boolean[] { false, false, false, true });

			// handle connection
			using (EFS.DB.Connection connection = EFS.RC.CC.CH.QRDConnection.New)
			{
				// exec create
				connection.DoExecuteNonQuery("[dbo].[SurveyCategories_Create]", parametersArray);
			}

			// get id value
			return parametersArray.GetOutPrameterValue<System.Int32?>("id"); 
		}

		/// Update
		public static void Update(
			System.Int32 id,
			System.Int32 surveyId,
			System.String name,
			System.Int32 priority)
		{			// handle connection
			using (EFS.DB.Connection connection = EFS.RC.CC.CH.QRDConnection.New)
			{
				connection.DoExecuteNonQuery(
					"[dbo].[SurveyCategories_Update]",
					new EFS.DB.Parameters.Array(
						new System.String[] { "id", "name", "survey_id", "priority" },
						new System.Object[] { id, name, surveyId, priority }));
			}
		}

		/// Delete
		public static void Delete(System.Int32 id)
		{
			// handle connection
			using (EFS.DB.Connection connection = EFS.RC.CC.CH.QRDConnection.New)
			{
				connection.DoExecuteNonQuery("[dbo].[SurveyCategories_Delete]", new EFS.DB.Parameters.Array("id", id));
			}
		}
	}

}